//
//  OMPaySuccessViewController.m
//  OMENGO_NEW
//
//  Created by 梁尚嘉 on 2017/5/12.
//  Copyright © 2017年 Kamfat. All rights reserved.
//

#import "OMPaySuccessViewController.h"
#import "OMPaySuccessInfoTableCell.h"
#import "OMPaySuccessPhoneTableCell.h"
#import "NSString+KTExt.h"
@interface OMPaySuccessViewController ()
@property (nonatomic, strong) UIButton *confirmButton;
@property (nonatomic, strong) UIButton *returnButton;
@end

@implementation OMPaySuccessViewController

- (void)backAction
{
    NaviPopToRoot(true);
}

- (void)viewDidLoad {

    self.loading = true;
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.fd_interactivePopDisabled = true;

    self.tableView.tableHeaderView = self.header;
    [self.tableView registerClass:[OMPaySuccessInfoTableCell class]
           forCellReuseIdentifier:[OMPaySuccessInfoTableCell cellIdentifier]];
    [self.tableView registerClass:[OMPaySuccessPhoneTableCell class]
           forCellReuseIdentifier:[OMPaySuccessPhoneTableCell cellIdentifier]];
    self.tableView.sectionFooterHeight = SM_SIZE(35);
    self.tableView.contentInset = UIEdgeInsetsMake(0, 0, SM_SIZE(130), 0);

    UIView *footer = [UIView new];
    footer.backgroundColor = SM_whiteColor;
    [self.view addSubview:footer];
    [footer mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(@(SM_SIZE(130)));
        make.left.right.bottom.equalTo(@0);
    }];
    [footer addSubview:self.returnButton];
    [self.returnButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(SM_SIZE(20)));
        make.top.equalTo(@(SM_SIZE(20)));
        make.bottom.equalTo(@(SM_SIZE(-20)));
    }];
    [footer addSubview:self.confirmButton];
    [self.confirmButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(@(SM_SIZE(20)));
        make.right.bottom.equalTo(@(SM_SIZE(-20)));
        make.left.equalTo(self.returnButton.mas_right).offset(SM_SIZE(15));
        make.width.equalTo(self.returnButton.mas_width);
    }];
    UIView *line = [UIView new];
    line.backgroundColor = SM_naviShadowColor;
    [footer addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.equalTo(@0);
        make.height.equalTo(@(.5f));
    }];


    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(.3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        self.loading = false;
        [self.tableView reloadData];
    });
}


- (UIView *)header
{
    UILabel *header = [UILabel new];
    header.text = @"恭喜您，下单成功!";
    header.font = SM_CFont(50);
    header.textColor = HEXCOLOR(@"#E94519");
    header.textAlignment = NSTextAlignmentCenter;
    header.frame = CGRectMake(0, 0, SM_SCREEN_WIDTH, SM_SIZE(200));
    return header;
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section) {
        return [OMPaySuccessPhoneTableCell tableViewCellWithTableView:tableView];
    }
    OMPaySuccessInfoTableCell *cell = [OMPaySuccessInfoTableCell tableViewCellWithTableView:tableView];
    [cell configureCellWithObj:self.order];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.isLoading) {
        return 0;
    }
    return [tableView fd_heightForCellWithIdentifier:indexPath.section?[OMPaySuccessPhoneTableCell cellIdentifier]:[OMPaySuccessInfoTableCell cellIdentifier] configuration:^(id cell) {
        [cell configureCellWithObj:self.order];
    }];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section) {
        [@"400-930-2220" makeCall];
    }
}


#pragma mark - Getters
- (UIButton *)confirmButton
{
    if (!_confirmButton) {
        _confirmButton = [UIButton new];
        [_confirmButton setTitle:@"查看订单详情" forState:UIControlStateNormal];
        [_confirmButton setTitleColor:SM_blackColor forState:UIControlStateNormal];
        [_confirmButton setBackgroundImage:[UIImage imageWithColor:SM_sangouColor] forState:UIControlStateNormal];
        [_confirmButton setBackgroundImage:[UIImage imageWithColor:RGBACOLOR(0, 0, 0, .26)] forState:UIControlStateDisabled];
        _confirmButton.titleLabel.font = SM_CFont(35);
        [_confirmButton addTarget:self action:@selector(submitAction:) forControlEvents:UIControlEventTouchUpInside];
        _confirmButton.layer.cornerRadius = 4.f;
        _confirmButton.clipsToBounds = true;
        _confirmButton.layer.borderWidth = 1.f;
        _confirmButton.layer.borderColor = SM_blackColor.CGColor;
    }
    return _confirmButton;
}

- (UIButton *)returnButton
{
    if (!_returnButton) {
        _returnButton = [UIButton new];
        [_returnButton setTitle:@"返回首页" forState:UIControlStateNormal];
        [_returnButton setTitleColor:SM_blackColor forState:UIControlStateNormal];
        [_returnButton setBackgroundImage:[UIImage imageWithColor:SM_whiteColor] forState:UIControlStateNormal];
        [_returnButton setBackgroundImage:[UIImage imageWithColor:RGBACOLOR(0, 0, 0, .26)] forState:UIControlStateDisabled];
        _returnButton.titleLabel.font = SM_CFont(35);
        [_returnButton addTarget:self action:@selector(submitAction:) forControlEvents:UIControlEventTouchUpInside];
        _returnButton.layer.cornerRadius = 4.f;
        _returnButton.clipsToBounds = true;
        _returnButton.layer.borderWidth = 1.f;
        _returnButton.layer.borderColor = SM_blackColor.CGColor;
    }
    return _returnButton;
}


#pragma mark - Actions
- (void)submitAction:(UIButton *)button
{
    if (self.returnButton == button) {        //返回首页

        [self.navigationController popToRootViewControllerAnimated:false];
        [Navigator cancelLoginAction];

    }else if (self.confirmButton == button) { //查看详情

        NaviPush(@"OMOrderDetailTabsViewController", @{@"orderSn":self.order.sn});
    }
}
@end
